<?php
namespace Admin\Controller;

class AdminController extends CommonController
{

    /**
     * 管理员列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $where['status'] = 1;
            $list = $this->lists(M('Admin'), true, $where, 'admin_id DESC', 10);
            $group = getAdminGroup(false);
            $this->assign('list', $list);
            $this->assign('group', $group);
            $this->display();
        }

    }

    /**
     * 新增
     *
     * @param void
     * @return void
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.');
            
            if (empty($data['password'])) {
                $this->error('密码必须填写');
            }
            if (empty($data['user_name'])) {
                $this->error('登陆名必须填写');
            }
            if (empty($data['real_name'])) {
                $this->error('姓名必须填写');
            }
            
            if ($data['password'] != $data['confirm_password']) {
                $this->error('确认密码与密码不匹配');
            }
            
            if (strlen($data['password']) > 20 || strlen($data['password']) < 6) {
                $this->error('密码长度为6-20');
            }
            
            if (strlen($data['user_name']) > 20 || strlen($data['user_name']) < 3) {
                $this->error('登录名长度为3-20');
            }
            
            $admin = D('Admin');
            $result = $admin->where(array( 'user_name' => $data['user_name'] ))->find();
            if ($result) {
                $this->error('登陆名已存在');
            }
            
            $data['password'] = md5($data['password']);
            
            $result = $admin->update($data);
            if (false !== $result) {
                $this->success('新增成功', U('index'));
            } else {
                $error = $admin->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $group = getAdminGroup(false);
            $this->assign('group', $group);
            $this->display();
        }
    }

    /**
     * 编辑
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');

            if (empty($data['admin_id'])) {
                $this->error('非法数据');
            }
            
            if (empty($data['real_name'])) {
                $this->error('姓名必须填写');
            }
            
            if (! empty($data['password'])) {
                if ($data['password'] != $data['confirm_password']) {
                    $this->error('确认密码与密码不匹配');
                }
                
                if (strlen($data['password']) > 20 || strlen($data['password']) < 6) {
                    $this->error('密码长度为6-20');
                }
                $data['password'] = md5($data['password']);
            } else {
                unset($data['password']);
            }
            
            $admin = D('Admin');
            
            $result = $admin->update($data);
            if (false !== $result) {
                $this->success('修改成功', U('index'));
            } else {
                $error = $admin->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('Admin')->where('admin_id = ' . $id)->find();
            $group = getAdminGroup(false);
            $this->assign('group', $group);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'admin_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Admin')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }   
    
    
    public function updatePassword()
    {
        if (IS_POST) {
            $data = I('post.');
            $admin = D('Admin');
            $adminID = isAdminLogin();
            $info = $admin->where('admin_id = ' . $adminID)->find();
            if ($info['password'] != md5($data['old_password'])){
                $this->error('原密码不对');
            }
            $data['admin_id'] = $adminID;
            $data['password'] = md5($data['password']);
            $result = $admin->update($data);
            if (false !== $result) {
                $this->success('修改成功', U('index/index'));
            } else {
                $error = $admin->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {

            $this->display();
        }
    }
    
}